package com.woniuxy.qiantai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.qiantai.entity.Comment;
import com.woniuxy.qiantai.entity.Orderitem;
import com.woniuxy.qiantai.entity.Product;
import com.woniuxy.qiantai.mapper.CommentMapper;
import com.woniuxy.qiantai.mapper.OrderitemMapper;
import com.woniuxy.qiantai.mapper.ProductMapper;
import com.woniuxy.qiantai.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.qiantai.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    OrderitemMapper orderitemMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public Page<Comment> selectCommentByshopId(Integer currentPage, Integer pageSize,Integer shopId) {
        Page<Comment> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (shopId != null){
            queryWrapper.eq("shop_id",shopId);
        }

        Page<Comment> commentPage = commentMapper.selectPage(page,queryWrapper);
        return commentPage;
    }

    @Override
    public List<Comment> queryComments(Integer shopId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);
        List listCom = commentMapper.selectList(queryWrapper);
        return listCom;
    }

    @Override
    public List<OrderItemVo> selectCommentByOrderId(Integer orderId) {
        QueryWrapper<Orderitem> orderitemQueryWrapper = new QueryWrapper<>();
        orderitemQueryWrapper.eq("order_id", orderId);
        List<Orderitem> orderitems = orderitemMapper.selectList(orderitemQueryWrapper);
        //将Orderitem集合封装成OrderItemVo集合
        List<OrderItemVo> orderItemVoList = new ArrayList<>();
        for (Orderitem orderitem : orderitems) {
            //将Orderitem对象封装成OrderItemVo对象
            OrderItemVo orderItemVo = new OrderItemVo();
            Product product = productMapper.selectById(orderitem.getProductId());
            orderItemVo.setProductName(product.getName());
            orderItemVo.setProductImgSrc(product.getImgSrc());
            orderItemVo.setId(orderitem.getId());
            orderItemVo.setUserId(orderitem.getUserId());
            orderItemVo.setProductId(orderitem.getProductId());
            orderItemVo.setPrice(orderitem.getPrice());
            orderItemVo.setCount(orderitem.getCount());
            orderItemVo.setOrderId(orderitem.getOrderId());
            orderItemVo.setStutus(orderitem.getStutus());
            orderItemVo.setShopId(orderitem.getShopId());
            //将OrderItemVo对象加入集合
            orderItemVoList.add(orderItemVo);
        }
        return orderItemVoList;

    }

    @Override
    public int insertCommentAndRate(String comment, Double rate,Integer orderId,Integer shopId,Integer userId) {
        Comment comment1 = new Comment();
        comment1.setOrderId(orderId);
        comment1.setShopId(shopId);
        comment1.setUserId(userId);
        comment1.setComments(comment);
        comment1.setGrade(rate);
        comment1.setCreateTime(new Date());
        int insert = commentMapper.insert(comment1);
        return insert;
    }
}
